#!perl -T

use strict;
use warnings;
use Test::More;
use Date::Simple qw/date today ymd/;

BEGIN {
	use_ok( 'NaturalDateGuesser' );
}

my $parser = NaturalDateGuesser->new();

my $today = today();
my $day_of_week = $today->day_of_week;


my $list = [
[ "Today", $today ],
[ "Tomorrow", $today + 1 ],
[ "Yesterday", $today - 1 ],
[ "Monday", $today + ((8 - $day_of_week)%7) ],
[ "Tuesday", $today + ((9 - $day_of_week)%7) ],
[ "Wednesday", $today + ((10 - $day_of_week)%7) ],
[ "Thursday", $today + ((11 - $day_of_week)%7) ],
[ "Friday", $today + ((12 - $day_of_week)%7) ],
[ "Saturday", $today + ((13 - $day_of_week)%7) ],
[ "Sunday", $today + ((7 - $day_of_week)%7) ],

[ "23rd June", ymd($today->year, 6, 23) ],
[ "23rd Jun", ymd($today->year, 6, 23) ],
[ "23 Jun", ymd($today->year, 6, 23) ],

[ "Next Monday", $today + next_calc($day_of_week, 8) ],
[ "Next Tuesday", $today + next_calc($day_of_week, 9) ],
[ "Next Wednesday", $today + next_calc($day_of_week, 10) ],
[ "Next Thursday", $today + next_calc($day_of_week, 11) ],
[ "Next thursday", $today + next_calc($day_of_week, 11) ],
[ "Next Friday", $today + next_calc($day_of_week, 12) ],
[ "Next Saturday", $today + next_calc($day_of_week, 13) ],
[ "Next Sunday", $today + next_calc($day_of_week, 7) ],

[ "Last Monday", $today + last_calc($day_of_week, 1) ],
[ "Last Tuesday", $today + last_calc($day_of_week, 2) ],
[ "Last Wednesday", $today + last_calc($day_of_week, 3) ],
[ "Last Thursday", $today + last_calc($day_of_week, 4) ],
[ "Last Friday", $today + last_calc($day_of_week, 5) ],
[ "Last Saturday", $today + last_calc($day_of_week, 6) ],
[ "Last Sunday", $today + last_calc($day_of_week, 0) ],

[ "In 1 day", $today + 1 ],
[ "In 2 days", $today + 2 ],
[ "In 10 days", $today + 10 ],
[ "In 1 week", $today + 7 ],
# TODO: document these because they could be open
# to interpretation.  I am taking next week to be
# in 7 days not next monday or whatever.
[ "Next week", $today + 7 ], # what should this be?
[ "In 3 weeks", $today + 7*3 ],
];

plan tests => scalar @$list;

foreach my $item (@$list)
{
	my ($string, $expected) = @$item;
	is($parser->parseDate($string), $expected, 
		"Parse $string");
}

sub last_calc
{
	my($day_of_week, $base) = @_;
	my $result = $base - $day_of_week;
	if($result >= 0)
	{
		$result -= 7;
	}

	return $result;
}

sub next_calc
{
	my ($day_of_week, $base) = @_;
	my $result = ($base - $day_of_week) % 7;
	if(!$result)
	{
		$result = 7;
	}
	return $result;
}
